package cn.rui.decoration.controller;

import cn.rui.decoration.mapper.CommentMapper;
import cn.rui.decoration.pojo.dto.CommentDTO;
import cn.rui.decoration.pojo.entity.Comment;
import cn.rui.decoration.pojo.vo.CommentDetailVO;
import cn.rui.decoration.pojo.vo.CommentListVO;
import cn.rui.decoration.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired(required = false)
    CommentMapper mapper;

    @RequestMapping("/insert")
    public int insert(@RequestBody CommentDTO comment, HttpSession session){
        UserVO user = (UserVO) session.getAttribute("user");

        System.out.println("comment = " + comment + ", session = " + session);
        //创建微博实体类 并且把DTO里面的数据装进实体类
        Comment w = new Comment();
        BeanUtils.copyProperties(comment,w);
        System.out.println(w);
        //设置当前时间
        w.setCreated(new Date());
        //设置当前登录用户的id
        w.setUserId(user.getId());
        mapper.insert(w);
        return 1;
    }
    @RequestMapping("/select")
    public List<CommentListVO> select(){
        return mapper.select();
    }
    @RequestMapping("/selectById")
    public CommentDetailVO selectById(int id){
        return mapper.selectById(id);
    }

}
